// pages/store/card/detail/detail.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: getApp().globalData.CustomBar,
    scrollHeight: '100vh',
    cardName: "",
    userList: [],
    isQryNextPage: true,
    qryParam: {
      cardId: null,
      keyWord: null,
      current: 1,
      size: 10,
    },
  },
  onScrollToLower() {
    if (this.data.isQryNextPage) {
      this.queryUserCount(true);
    }
  },
  onScroll(event) {
    let isScrollEnd = false;
    if (event.detail.scrollTop > 10) {
      isScrollEnd = true;
    }
    if (isScrollEnd != this.data.isScrollEnd) {
      this.setData({ isScrollEnd: isScrollEnd })
    }
    this.setData({ scrollTop: event.detail.scrollTop });
  },
  onSearch(event) {
    this.setData({ 'qryParam.keyWord': event.detail, isQryNextPage: true })
    this.queryUserCount();
  },
  onClear() {
    this.setData({ 'qryParam.keyWord': '', isQryNextPage: true })
    this.queryUserCount();
  },
  queryUserCount(isNextPage) {
    Toast.loading({
      message: '加载中...',
      forbidClick: true,
    });
    let isQryNextPage = false;
    if (isNextPage && this.data.userList.length >= this.data.qryParam.current * this.data.userList.size) {
      isQryNextPage = true;
      this.setData({ 'qryParam.current': this.data.qryParam.current + 1 })
    } else if (!isNextPage) {
      this.setData({ 'qryParam.current': 1 })
    }
    getApp().callContainer('/api/store/card/queryUserCount', this.data.qryParam, (res) => {
      this.setData({ userList: res.data.data.records || [] });
      if (res.data.data.records && res.data.data.records.length > 0) {
        for (const user of res.data.data.records) {
          if (isQryNextPage) {
            // 如果查询结果小于查询条数，则下一次不继续分页查询
            if (res.data.data.records.length < this.data.qryParam.size) {
              this.setData({ isQryNextPage: false })
            }
            this.data.userList.push(user);
          }
        }
      }
      if (isNextPage) {
        this.setData({ userList: this.data.userList });
      } else {
        this.setData({ userList: res.data.data.records || [] });
      }
      this.loadTmpFile();
      Toast.clear();
    }, () => {
      Toast.clear();
    })
  },
  // 缓存图片
  loadTmpFile() {
    for (const user of this.data.userList) {
      if (!user.avatar) {
        continue;
      }
      getApp().getTempFilePath(user.avatar).then((res) => {
        user.tmpAvatar = res;
        this.setData({ userList: this.data.userList });
      })
    }
  },
  // 跳转到卡片详情
  onClickCardDetl(event) {
    const item = event.currentTarget.dataset.operation;
    wx.navigateTo({
      url: "/pages/store/card/detail/detail?cardId=" + this.data.qryParam.cardId + "&userId=" + item.userId + "&cardName=" + this.data.cardName,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.cardId) {
      this.setData({ 'qryParam.cardId': options.cardId, cardName: options.cardName });
      this.queryUserCount();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.queryUserCount();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})